package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.AddressVo;
import com.itheima.service.AddressService;
import com.itheima.utils.QiNiuYunUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("address")
public class AddressController {
    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;
    //查询地址(分页查询)
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean param){
        PageResult pageResult= addressService.findAllAddressByPage(param);
        return   new Result(true,"查询所有的地址信息成功",pageResult);

    }

    //添加地址
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        addressService.add(address);
        if (address.getImg()!=null) {
            //添加图片地址到redis中(排重)
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,address.getImg());
        }
        return new Result (true,"添加地址成功");
    }
    //上传图片
    @RequestMapping("/upload")
    public Result upload(@RequestParam MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();//获取文件原始名
        String extention = originalFilename.substring(originalFilename.lastIndexOf(".")-1);//这里就是后缀
        //添加uuid
        String fileName = UUID.randomUUID().toString() + extention;//文件名
        System.out.println(fileName);
        String dbimg;
        try {
            dbimg = QiNiuYunUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将信息存到reids,防止垃圾数据(带网址)
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,dbimg);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        //图片上传成功
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,dbimg);
    }

    //保存修改后的地址信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address){
        addressService.edit(address);
        //添加到redis中排重
        if (address.getImg()!=null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,address.getImg());
        }
        return new Result (true,"保存修改后地址信息成功");
    }

    //删除地址信息(包含图片的删除(云端))
    @RequestMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id,@RequestBody String imgUrl){
        System.out.println("地址图片url"+imgUrl);
        addressService.deleteById(id);
        //将要删除的地址加到redis中(定时删除云端)
        jedisPool.getResource().sadd(RedisConstant.Delete_PIC_DB_RESOURCES,imgUrl);
        return new Result(true,"删除地址信息成功");
    }
    //[{"name":"蓝泊湾酒店","lat":"33.46917263305","longt":"118.22015188956"},{"name":"希尔顿宾馆","lat":"33.463897899313764","longt":"118.22722780998242"}]
    //获取所有的标点数据(返回格式特定)
    @RequestMapping("/getAllAddress")
    public Result getAllAddress(){
        List<AddressVo> list= addressService.getAllAddress();
        return   new Result(true,"查询标点数据成功",list);
    }
}
